﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;

namespace AgLux
{
    /// <summary>
    /// Represents a control that displays a course introduction.
    /// </summary>

    public class Introduction : Control
    {
        public event EventHandler Leave;

        private Canvas _mainArea;
        private TextBlock _title, _description;
        private Canvas _startButton;

        /// <summary>
        /// Initializes a new instance of the Introduction class.
        /// </summary>

        /// <param name="title">
        /// The title of the course.
        /// </param>

        /// <param name="description">
        /// The description of the course.
        /// </param>

        public Introduction(string title, string description)
            : base()
        {
            FrameworkElement resource = InitializeFromXaml(new StreamReader(GetType().Assembly.GetManifestResourceStream("eLearning.Introduction.xaml")).ReadToEnd());

            _mainArea = (Canvas)resource.FindName("mainArea");
            _title = (TextBlock)resource.FindName("title");
            _description = (TextBlock)resource.FindName("description");
            _startButton = (Canvas)resource.FindName("startButton");

            _mainArea.Width = BrowserHost.ActualWidth;
            _mainArea.Height = BrowserHost.ActualHeight;

            _title.Text = title;
            _title.Width = _title.ActualWidth;
            _title.Height = _title.ActualHeight;

            _description.Text = description;
            _description.Width = _mainArea.Width - 480;
            _description.Height = _description.ActualHeight;

            _title.SetValue(Canvas.LeftProperty, _mainArea.Width / 2 - _title.Width / 2);
            _title.SetValue(Canvas.TopProperty, _mainArea.Height / 2 - (_title.Height + _description.Height + _startButton.Height + 90) / 2);

            _description.SetValue(Canvas.LeftProperty, _mainArea.Width / 2 - _description.Width / 2);
            _description.SetValue(Canvas.TopProperty, _mainArea.Height / 2 - (_title.Height + _description.Height + _startButton.Height + 90) / 2 + _title.Height + 30);

            _startButton.SetValue(Canvas.LeftProperty, _mainArea.Width / 2 - _startButton.Width / 2);
            _startButton.SetValue(Canvas.TopProperty, _mainArea.Height / 2 - (_title.Height + _description.Height + _startButton.Height + 90) / 2 + _title.Height + _description.Height + 90);
            _startButton.MouseLeftButtonDown += new MouseEventHandler(StartButtonPressed);
        }

        /// <summary>
        /// Occurs when the start button is pressed.
        /// </summary>

        /// <param name="sender">
        /// The object that raised the event.
        /// </param>

        /// <param name="args">
        /// The MouseEventArgs that contains the event data.
        /// </param>

        private void StartButtonPressed(object sender, MouseEventArgs args)
        {
            OnLeave(new EventArgs());
        }

        /// <summary>
        /// Raises the Leave event.
        /// </summary>

        /// <param name="args">
        /// The EventArgs that contains the event data.
        /// </param>

        private void OnLeave(EventArgs args)
        {
            if (Leave != null)
            {
                Leave(this, args);
            }
        }
    }
}
